package com.dex4d.utils;


import android.content.Context;
import android.content.SharedPreferences;

import com.dex4d.constants.AppConstants;

import java.math.BigInteger;

//轻量级数据保存类
// 使用sharePreference保存数据到文件
// 只适用简单的数据
public class SPUtil {

    private static Context context;

    public static void setContext(Context ctx) {
        context = ctx;
    }

    public static void putString(String key, String value) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static String getString(String key, String defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getString(key, defaultValue);
    }

    public static String getString(String key) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getString(key, "");
    }

    public static void putBoolean(String key, boolean value) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static boolean getBoolean(String key) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getBoolean(key, false);
    }

    public static void putLong(String key, long value) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public static long getLong(String key) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getLong(key, 0);
    }

    public static void putInt(String key, int value) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static int getInt(String key) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getInt(key, 0);
    }

    public static int getInt(String key, int defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getInt(key, defaultValue);
    }

    public static void putFloat(String key, float value) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public static float getFloat(String key) {
        SharedPreferences settings = context.getSharedPreferences(AppConstants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getFloat(key, 0);
    }

    /**
     *
     * @param remoteNonce
     */
    public static void increaseNonce(BigInteger remoteNonce) {
        BigInteger localNonce = getLocalNonce(remoteNonce);
        putString(AppConstants.SP_NONCE_KEY, localNonce.add(BigInteger.ONE).toString());
    }

    /**
     *
     * @param remoteNonce
     * @return
     */
    public static BigInteger getLocalNonce(String remoteNonce) {
        return getLocalNonce(new BigInteger(remoteNonce));
    }

    /**
     *
     * @param remoteNonce
     * @return
     */
    public static BigInteger getLocalNonce(BigInteger remoteNonce) {
        BigInteger localNonce = new BigInteger(getString(AppConstants.SP_NONCE_KEY, "0"));
        return localNonce.max(remoteNonce);
    }

}
